package com.jic.product.controller.manage;

import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.product.common.util.BeanUtil;
import com.jic.product.entity.Warehouse;
import com.jic.product.manage.request.WarehouseRequest;
import com.jic.product.manage.response.WarehouseResponse;
import com.jic.product.service.WarehouseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by HeJun on 2020/1/18
 *
 * @Description:
 */
@Api(value = "仓库管理controller", tags = "仓库管理", description = "WarehouseController")
@RestController
@RequestMapping("/warehouseManagement")
@Slf4j
public class WarehouseController {

    @Autowired
    WarehouseService warehouseService;

    @ApiOperation(value = "获取仓库信息分页", notes = "获取仓库信息分页")
    @PostMapping("/queryPage")
    public RestResult<PageResult<WarehouseResponse>> queryPage(@RequestBody WarehouseRequest form) {
        Page page = new Page();
        page.setLimit(form.getLimit());
        page.setStart(form.getStart());

        PageResult<WarehouseResponse> result = warehouseService.queryPage(form, page);

        List<WarehouseResponse> list = result.getRows().stream().filter(item -> item != null).map(temp -> {
            WarehouseResponse bmr = BeanUtil.copy(temp, WarehouseResponse.class);
            return bmr;
        }).collect(Collectors.toList());
        PageResult<WarehouseResponse> pageResult = new PageResult<>();

        pageResult.setRows(list);
        pageResult.setTotal(result.getTotal());
        return RestResult.success(pageResult);
    }

    @ApiOperation(value = "获取仓库信息", notes = "获取仓库信息")
    @PostMapping("/query")
    public RestResult<List<Warehouse>> query(@RequestBody WarehouseRequest form) {
        Warehouse warehouse = BeanUtil.copy(form, Warehouse.class);
        List<Warehouse> result = warehouseService.query(warehouse);
        return RestResult.success(result);
    }

    @ApiOperation(value = "新增仓库", notes = "新增仓库")
    @PostMapping("/insert")
    public RestResult<Long> insert(@RequestBody WarehouseRequest t) {
        long result = 0;
        try {
            Warehouse wh = BeanUtil.copy(t, Warehouse.class);
            if (0 == t.getId()) {
                result = warehouseService.insert(wh);
            } else {
                result = warehouseService.update(wh);
            }
        } catch (Exception e) {
            log.info("warehouseManagement ==> insert or update ==>,e:{}",e.getLocalizedMessage());
            return RestResult.error("9999","insert or update");
        }
        return RestResult.success(result);
    }
}
